
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       bl_protocol.h
  * @author     baiyang
  * @date       2023-4-23
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <stdint.h>
#include <stdbool.h>

#include <rtconfig.h>

#include "support.h"
/*-----------------------------------macro------------------------------------*/
#define BL_WAIT_MAGIC    0x19710317        /* magic number in PWR regs to wait in bootloader */

/* generic timers */
#define NTIMERS        4
#define TIMER_BL_WAIT    0
#define TIMER_CIN    1
#define TIMER_LED    2
#define TIMER_DELAY    3


/* LEDs */
#define LED_ACTIVITY    1
#define LED_BOOTLOADER    2

#ifdef BOOT_DELAY_ADDRESS
# define BOOT_DELAY_SIGNATURE1    0x92c2ecff
# define BOOT_DELAY_SIGNATURE2    0xc5057d5d
# ifndef BOOT_DELAY_MAX
#   define BOOT_DELAY_MAX        30
# endif
#endif

#define MAX_DES_LENGTH 20

#define arraySize(a) (sizeof((a))/sizeof(((a)[0])))
/*----------------------------------typedef-----------------------------------*/
/*****************************************************************************
 * Generic bootloader functions.
 */

/* enum for whether bootloading via USB or USART */
enum {
    NONE,
    USART,
    USB
};

/* board info forwarded from board-specific code to booloader */
struct boardinfo {
    uint32_t    board_type;
    uint32_t    board_rev;
    uint32_t    fw_size;
    uint32_t    extf_size;
} __attribute__((packed));
/*----------------------------------variable----------------------------------*/
extern struct boardinfo board_info;
extern volatile unsigned timer[NTIMERS];    /* each timer decrements every millisecond if > 0 */
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void jump_to_app(void);
void bootloader(unsigned timeout);
void delay(unsigned msec);

/* generic receive buffer for async reads */
void buf_put(uint8_t b);
int buf_get(void);

extern uint32_t board_get_devices(void);
/*****************************************************************************
 * Interface in/output.
 */

extern void cinit(void *config, uint8_t interface);
extern void cfini(void);
extern int cin(uint32_t devices);
extern void cout(uint8_t *buf, unsigned len);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



